// pages/doctor-case-add-4/doctor-case-add-4.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    imagesurl: [], // 上传成功的图片地址
    content: '', // 详细信息
    options: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    this.setData({
      'options': options,
    })
  },

  // 添加补充信息
  addContent: function(e) {
    var content = e.detail.value;
    this.setData({
      'content': content,
    })
    console.log(this.data.content);

  },

  // 上传图片
  chooseImg: function(e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 将现有和新上传的合并
        var images = that.data.images.concat(res.tempFilePaths); // 合并原有数组和现传照片
        images = images.length <= 5 ? images : images.slice(0, 5); // 取前5张
        that.setData({
          'images': images,
        })
        that.uploadImages();
      },
    })
  },


  //  预览图片
  handleImagePreview: function(e) {
    var index = e.target.dataset.index;
    var images = this.data.images;
    wx.previewImage({
      current: images[index],
      urls: images,
    })
  },

  // 删除图片
  handleDeleteImage: function(e) {
    var index = e.target.dataset.index;
    var images = this.data.images;
    images.splice(index, 1);
    console.log(images);
    this.setData({
      'images': images,
    })
  },

  // 上传图片
  uploadImages: function(e) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    });
    var images = this.data.images;
    var imagesArr = [];
    for (var i = 0; i < images.length; i++) {
      wx.uploadFile({
        url: app.globalData.baseUrl + 'doctor/product/uploadFile',
        filePath: images[i],
        name: 'file',
        header: {
          'sign': app.globalData.sign,
          'token': app.globalData.token,
          "Content-Type": "multipart/form-data"
        },
        success: function(res) {
          console.log('上传结果:', res);
          var path = JSON.parse(res.data).data[0].filePath;
          imagesArr.push(path);
          console.log('数组:', imagesArr)
          if (that.data.imagesurl.length < 5){
            that.data.imagesurl.push(path);
          }
          // that.data.imagesurl.push(path);
          // console.log('总数组:', that.data.imagesurl)          
        }
      })
    };
  },

  // 完成案例添加
  completeAddCase: function (e) { 
    if (!this.data.content){
      wx.showToast({
        title: '请添加补充信息',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.images.length == 0) {
      wx.showToast({
        title: '请添加图片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    this.handleSubmit();
  },

  handleSubmit: function (e) {
    console.log('最终图片数组:', this.data.imagesurl);
    var that = this;
    var oneType = this.data.options.choosedid.split(",")[0];
    var twoType = this.data.options.choosedid.split(",")[1];
    var threeType = this.data.options.choosedid.split(",")[2];
    console.log('一级分类:', oneType);
    console.log('二级分类:', twoType);
    console.log('三级分类:', threeType);
    var goodsId = this.data.options.goodsId;
    var imagesurl = this.data.imagesurl;
    var content = this.data.content;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/docExample/create',
      data: {
        'oneType': oneType,
        'twoType': twoType,
        'threeType': threeType,        
        'goodsId': goodsId,
        'pic': imagesurl,
        'content': content,
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('上传商品结果:', res)
        console.log('上传商品结果:', res.data)
        console.log('上传商品结果:', res.data.data)
        if (res.data.status_code == 200){
          wx.redirectTo({
            url: '../doctor-case-add-5/doctor-case-add-5?type=1',
          })
          wx.request({
            url: app.globalData.baseUrl + 'doctor/memberTaskApi/addBeauty',
            data: {
              userId: wx.getStorageSync('userid'),
              type: 2
            },
            header: {
              'sign': app.globalData.sign,
              'token': app.globalData.token,
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
            method: 'POST',
            success(res) {
              console.log('给美丽币给美丽值',res);
            }
          })
        }      
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})